package com.gexin.controller.system.shop;

import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.shop.ShopFile;
import com.gexin.model.shop.ShopFileTag;
import com.gexin.model.shop.ShopInfo;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;

/**
 * 店铺图片素材管理
 * @author 肖德明
 *  2016-06-28
 *
 */
@ControllerBind(controllerKey = "/system/file", viewPath = "WEB-INF/system/")
public class ShopFileManageController extends SystemBaseController {
	//private static Log log = Log.getLog(ShopFileManageController.class);
	public void index(){
		//一个店铺的轮播图不会太多,
		setAttr("tag_list", ShopFileTag.dao.getTagByShopId(ShopInfo.shop_id));
		Integer tag_id=getParaToInt("tag_id", 1);
		setAttr("tag_id", tag_id);
		paginate(ShopFile.dao.getImglistPaginate(tag_id, ShopInfo.shop_id,getpage(),20));
	    render("File_Manage.html");
	}
	
	/**移动项目标签 /shangjia/file/move_tag?tag_id=1&img_id=1,1,1,2,3 **/
	public void move_tag(){
		Integer tag_id=getParaToInt("tag_id");
		String img_id=getPara("img_id");
		Db.update(String.format("update  t_shop_file set tag_id=%s where file_id in(%s)", tag_id,img_id));
		success();
	}
	
	/**更新分组信息  /shangjia/file/update_tag/id?tag_name=*/
	public  void update_tag(){
		ShopFileTag tag = ShopFileTag.dao.findById(getParaToInt());
		if(tag==null){
			fail("文件不存在！");
			return ;
		}
		if(!tag.getShopId().equals(ShopInfo.shop_id)){
			fail("非法访问！");
			return ;
		}
		String  tag_name  =getPara("tag_name");
		tag.setTagName(tag_name);
		if(tag.update()){
			success(tag);
		}else{
			fail("请稍后再试！");
		}
	}
	
	/**删除分组信息  /shangjia/file/del_tag/id/type=1**/
	public  void del_tag(){
		Integer type  = getParaToInt("type",1);
		ShopFileTag tag = ShopFileTag.dao.findById(getParaToInt());
		if(tag==null){
			fail("分组不存在！");
			return ;
		}
		if(!tag.getShopId().equals(ShopInfo.shop_id)){
			fail("非法访问！");
			return ;
		}
		if(tag.delete()){
			if(type==2){
				Db.update("update  t_shop_file set tag_id=1 where tag_id=?",tag.getTagId());//把这个分组的文件移动到默认那个文件组里面去
			}else{
				Db.update("DELETE from t_shop_file  where shop_id=? and tag_id=?", ShopInfo.shop_id,tag.getTagId());
			}
			success();
		}else{
			fail("请稍后再试！");
		}
	}
	
	/**新增标签 /shangjia/file/add_tag?tag_name=test**/
	public void add_tag(){
		String  tag_name= getPara("tag_name");
		if(Db.queryLong("select count(1) from  t_shop_file_tag  where tag_name=? and shop_id=?",tag_name, ShopInfo.shop_id)>0){
			fail("标签名称已经存在！");
			return ;
		}
		if(StringUtil.isNotBlank(tag_name)){
			ShopFileTag tag  =new ShopFileTag();
			tag.setShopId(ShopInfo.shop_id);
			tag.setTagName(tag_name);
			if(tag.save()){
				success(tag);
			}else{
				fail();
			}
		}else{
			fail("请输入标签名称！");
		}
	}
	/**删除文件 /shangjia/file/del_file?file_id=1,1,2**/
	public void del_file(){
		 Db.update("update t_shop_file set tag_id=1  where file_id in("+getPara("file_id")+")  and shop_id=?", ShopInfo.shop_id);
		 int count = Db.update("DELETE from t_shop_file  where file_id in("+getPara("file_id")+")  and shop_id=?", ShopInfo.shop_id);
		 if(count>0){
			success(count);
		}else{
			fail("请稍后再试！");
		}
	}
	
}


